package com.x.linx.fenfencard;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.baidu.lbsapi.auth.i;
import com.x.linx.fenfencard.model.Config;
import com.x.linx.fenfencard.model.Manager;
import com.x.linx.fenfencard.model.meta.IRequestCallback;
import com.x.linx.fenfencard.model.meta.InfoOrganize;
import com.x.linx.fenfencard.model.meta.Location;
import com.x.linx.utils.D;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.AdapterView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

public class OrganizeListActivity extends Activity {
	protected static final String TAG = OrganizeListActivity.class
			.getSimpleName();

	private static final int INTERNET_FAILURE = -1;
	private static final int LOAD_SUCCESS = 1;
	private static final int LOAD_MORE_SUCCESS = 3;
	private static final int NO_MORE_INFO = 4;
	private static final int LOAD_NEW_INFO = 5;

	private RTPullListView pullListView;
	private ProgressBar moreProgressBar;

	// +
	private static final String ITEM_IMAGE = "ItemImage";
	private static final String ITEM_TITLE = "ItemTitle";
	private static final String ITEM_DESCRIPTION = "ItemDescription";
	private static final String ITEM_LAST_IMAGE = "LastImage";

	String[] mItemsName = new String[] { ITEM_IMAGE, ITEM_TITLE, ITEM_DESCRIPTION,
			ITEM_LAST_IMAGE };
	int[] mItemsId = new int[] { R.id.ItemImage, R.id.ItemTitle, R.id.ItemDescription, R.id.last };
	// 生成动态数组，加入数据
	ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();
	SimpleAdapter listItemAdapter;
	// -
	MyApp myApp;
	Manager manager;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		D.i(TAG, "onCreate()");
		myApp = (MyApp) getApplication();
		manager = myApp.getManager();

		setContentView(R.layout.demo_pull_refresh);
		pullListView = (RTPullListView) this.findViewById(R.id.pullListView);

		// +
		// 生成适配器的Item和动态数组对应的元素
		listItemAdapter = new SimpleAdapter(this, listItem, // 数据源
				R.layout.demo_list_item, // ListItem的XML实现
				mItemsName, // 动态数组与ImageItem对应的子项
				mItemsId); // ImageItem的XML文件里面的一个ImageView,两个TextView ID

		// 添加并且显示
		pullListView.setAdapter(listItemAdapter);

		// 添加点击
		pullListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				D.i(TAG, "点击第" + arg2 + "个项目");
			}
		});
		// -

		// 添加listview底部获取更多按钮（可自定义）
		LayoutInflater inflater = LayoutInflater.from(this);
		View view = inflater.inflate(R.layout.list_footview, null);
		RelativeLayout footerView = (RelativeLayout) view
				.findViewById(R.id.list_footview);
		moreProgressBar = (ProgressBar) view.findViewById(R.id.footer_progress);
		pullListView.addFooterView(footerView);

		// 获取数据
		refreshOrgList();

		// 下拉刷新监听器
		pullListView
				.setonRefreshListener(new RTPullListView.OnRefreshListener() {
					@Override
					public void onRefresh() {
						refreshOrgList();
					}
				});

		// 获取跟多监听器
		footerView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				moreProgressBar.setVisibility(View.VISIBLE);
				getMoreData(LOAD_MORE_SUCCESS);
			}
		});
	}
	
	void refreshOrgList(){
		manager.getOrganize().clearOrgList();
		listItem.clear();
		getMoreData(LOAD_NEW_INFO);
	}

	private void getMoreData(final int msg) {
		new Thread(new Runnable() {
			@Override
			public void run() {
				manager.getMoreOrgList(Location.newTestLocation(), Config.ORG_LIST_NUM,
						new IRequestCallback() {
							@Override
							public void success(Object returnValueObject) {
								List<InfoOrganize> list = (List<InfoOrganize>) returnValueObject;
								for (InfoOrganize infoOrganize : list) {
									HashMap<String, Object> map = new HashMap<String, Object>();
									map.put(ITEM_IMAGE, R.drawable.list_02);// 图像资源的ID
									map.put(ITEM_TITLE, infoOrganize.getName());
									map.put(ITEM_DESCRIPTION, infoOrganize.getDescription());
									map.put(ITEM_LAST_IMAGE, R.drawable.list_01);
									listItem.add(map);
								}
								myHandler.sendEmptyMessage(msg);
							}

							@Override
							public void fail(String errorMessage) {
								Toast.makeText(OrganizeListActivity.this,
										errorMessage, Toast.LENGTH_SHORT)
										.show();
							}
						});
			}
		}).start();
	}

	// 结果处理
	private Handler myHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case LOAD_MORE_SUCCESS:
				moreProgressBar.setVisibility(View.GONE);
				listItemAdapter.notifyDataSetChanged();
				pullListView.setSelectionfoot();
				break;

			case LOAD_NEW_INFO:
				listItemAdapter.notifyDataSetChanged();
				pullListView.onRefreshComplete();
				break;
			default:
				break;
			}
		}
	};
}